#include "xcrypt_task.h"
#include "xcrypt.h"
#include <iostream>

void XCryptTask::init(const std::string &password)
{
    enc_ = std::make_shared<XCrypt>();
    enc_->init(password);
}

void XCryptTask::setState(bool isEncrypt)
{
    isEncrypt_ = isEncrypt;
}

void XCryptTask::handle()
{
    while (!isExit_) {
        auto src = pop();
        auto dest = XData::make_data(memPool_);
        int outSize = src->size() + enc_->getPadding(src->size());
        dest->alloc(outSize);
        int dataSize = 0;
        bool isEnd = src->end();
        if (isEncrypt_) {
            dataSize = enc_->encypt((char *)src->data(), src->size(), 
                (char *)dest->data(), isEnd);
        } else {
            dataSize = enc_->Dencypt((char *)src->data(), src->size(), 
                (char *)dest->data(), isEnd);
        }
        dest->setSize(dataSize);
        dest->setEnd(isEnd);
        if (next_) {
            next_->push(dest);
        }
        std::cout << "handle data " << dataSize << std::endl; 
        if (dest->end()) {
            break;
        }
    }
}